package tree;


import java.util.*;

public class BinaryTreeZigzagLevelOrderTraversal103 {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        LinkedList<TreeNode> linkedList = new LinkedList();
        linkedList.add(root);
        boolean direct = true;
        while (linkedList.size() != 0) {
            int size = linkedList.size();
            LinkedList<Integer> res = new LinkedList<>();
            while (size-- != 0) {
                TreeNode node = linkedList.poll();
                if (node.left != null) {
                    linkedList.offer(node.left);
                }
                if (node.right != null) {
                    linkedList.offer(node.right);
                }
                if(direct){
                    res.offer(node.val);
                }else {
                    res.offerLast(node.val);
                }

            }

            direct = !direct;
            result.add(res);
        }
        return result;

    }

}
